<template>
  <div>
    <!-- 面包屑导航区 -->
    <hamburger :secTitle="secTitle" :thirdTitle="thirdTitle"></hamburger>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 添加按钮 -->
      <el-button type="info" plain class="btn" @click="addCate">添加分类</el-button>
      <!-- 分类表格 -->
      <tree-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted==false" style="color:#3366cc"></i>
          <i class="el-icon-error" v-else style="color:	#00CD00"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level==0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level==1">二级</el-tag>
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            @click="showEditDialog(scope.row)"
          >编辑</el-button>
          <el-button
            type="success"
            icon="el-icon-delete"
            size="small"
            @click="removeEditDialog(scope.row.cat_id)"
          >删除</el-button>
        </template>
      </tree-table>
      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryList.pagenum"
        :page-size="queryList.pagesize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <!-- 添加对话框 -->
      <el-dialog title="添加分类" :visible.sync="dialogVisible" width="50%" @close="addDialogClosed">
        <el-form label-width="100px" :rules="rules" :model="ruleForm" ref="ruleRef">
          <el-form-item label="分类名称:" prop="cat_name">
            <el-input v-model="ruleForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类:" prop="cat_pid">
            <!-- options:指定数据源 -->
            <!-- props：指定配置对象 -->
            <el-cascader
              v-model="selectedKeys"
              :options="parentCateList"
              size="medium"
              :props="cascaderProps"
              expandTrigger="hover"
              @change="parentCateChanged"
              clearable
              change-on-select
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCateSure">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 修改对话框 -->
      <el-dialog title="修改分类" :visible.sync="editDialogVisible" width="50%">
        <el-form label-width="100px" :rules="rules" :model="editRuleForm" ref="editRuleRef">
          <el-form-item label="分类名称:" prop="cat_name">
            <el-input v-model="editRuleForm.cat_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editCate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      secTitle: '商品管理',
      thirdTitle: '商品分类',
      cateList: [],
      queryList: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      dialogVisible: false,
      ruleForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      rules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      //保存1，2级父级分类的列表
      parentCateList: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover'
      },
      selectedKeys: [],
      editDialogVisible: false,
      editRuleForm: {
        cat_name: '',
        cat_pid: 0,
        cat_id:''
      }
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    //获取商品分类数据
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryList
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败')
      }
      //console.log(res.data)
      this.cateList = res.data.result
      this.total = res.data.total
    },
    addCate() {
      //获取分类列表
      this.getParentList()
      this.dialogVisible = true
    },
    handleSizeChange(newSize) {
      this.queryList.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(newNum) {
      this.queryList.pagenum = newNum
      this.getCateList()
    },
    //获取父级分类列表
    async getParentList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类失败')
      }
      // console.log(res.data)
      this.parentCateList = res.data
    },
    parentCateChanged() {
      console.log(this.selectedKeys)
      if (this.selectedKeys.length > 0) {
        this.ruleForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        // console.log(this.ruleForm.cat_pid)
        this.ruleForm.cat_level = this.selectedKeys.length
      } else {
        this.ruleForm.cat_pid = 0
        this.ruleForm.cat_level = 0
      }
    },
    addCateSure() {
      this.$refs.ruleRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.ruleForm)
        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败')
        } else {
          this.$message.success('添加分类成功')
          this.getCateList()
          this.dialogVisible = false
        }
      })
    },
    addDialogClosed() {
      this.selectedKeys = []
    },
    async showEditDialog(cate) {
      
      this.editDialogVisible = true
      const { data: res } = await this.$http.get('categories/' + cate.cat_id)
      if (res.meta.status !== 200) {
        return this.$message.error('获取分类信息失败')
      } else {
        this.$message.success('获取分类信息成功')
        this.editRuleForm = res.data
      }
    },
    editCate(){
      this.$refs.editRuleRef.validate(async valid => {
        if (!valid) return this.$message.error('请填写完整的角色信息')
        const { data: res } = await this.$http.put(
          'categories/' + this.editRuleForm.cat_id,
          this.editRuleForm
        )
        // console.log(res)
        // console.log(typeof this.editRoleForm.roleId)
        if (res.meta.status !== 200) {
          return this.$message.error('修改角色失败')
        } else {
          this.$message.success('修改角色成功')
          this.getCateList()
          this.editDialogVisible = false
          
        }
      })
    },
    async removeEditDialog(id){
       const confirmRes = await this.$confirm(
        '此操作将永久删除该分类, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirmRes !== 'confirm') {
        return this.$message.info('取消删除角色')
      }
      const { data: res } = await this.$http.delete('categories/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除角色失败')
      } else {
        this.$message.success('删除角色成功')
        this.getCateList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.btn {
  margin-bottom: 20px;
}
.el-cascader {
  width: 100%;
}
</style>